﻿//=============================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 作 者：孙德响
// 日 期：2011-12-23
// 描 述: 销售规划管理
//
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00   2011/12/23    孙德响           
//=============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.EHR.Library;
using ZX.BLL.Extend;
using ZX.CRM.Library;

namespace ZX.WebApp.Crm.Sale
{
    public partial class AddLayout : ListPage

    {
        BaseBussnies Bbussness = new BaseBussnies();  //业务处理类
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(ZX.WebApp.Crm.Sale.AddLayout));
            if (!IsPostBack)
            {
                SetDropYear();
                Review();
            }

        }
        /// <summary>
        /// 加载DDL
        /// </summary>
        private void SetDropYear()
        {
            int year = DateTime.Now.Year;
            for (int i = year - 10; i < year + 10; i++)
            {
                ddlyear.Items.Add(i.ToString());
            }
            ddlyear.SelectedValue = year.ToString();
        }
        /// <summary>
        /// 显示
        /// </summary>
        private void Review()
        {
            string id = this.GetStringFromQueryString("ID", "");        //课程活动的主键  
            DatUser du = (DatUser)System.Web.HttpContext.Current.Session["User"]; //用户ID
            if (du != null)
            {
                int types = int.Parse(this.GetStringFromQueryString("type", "1"));    //备注 1：新增规划  2：做编辑规划   3:查看
                type.Value = types.ToString();
                if (types == 1)   //新增
                {
                    this.txtusername.Value = du.UName;                        //当前用户
                    this.txtaddtime.Value = DateTime.Now.ToString("yyyy-MM-dd  HH:mm");   
                }
                else if (types == 2)  //修改
                {
                    if (id.Trim().Length > 0)
                    {

                        SetControl(id);
                    }
                }
                else if (types == 3)
                {
                    if (id.Trim().Length > 0)
                    {
                        SetControl(id);
                        //this.txtExplain1.Disabled = true;
                        //this.txtExplain2.Disabled = true;
                        //this.txtExplain4.Disabled = true;
                        //this.txtExplain3.Disabled = true;
                        //this.txtExplain5.Disabled = true;
                        //this.txtExplain6.Disabled = true;
                        //this.txtExplain7.Disabled = true;
                        //this.txtExplain8.Disabled = true;
                        //this.txtExplain9.Disabled = true;
                        //this.txtExplain10.Disabled = true;
                        //this.txtExplain11.Disabled = true;
                        //this.txtExplain12.Disabled = true;
                        //this.TxtMonths1.Disabled = true;
                        //this.TxtMonths2.Disabled = true;
                        //this.TxtMonths3.Disabled = true;
                        //this.TxtMonths4.Disabled = true;
                        //this.TxtMonths5.Disabled = true;
                        //this.TxtMonths6.Disabled = true;
                        //this.TxtMonths7.Disabled = true;
                        //this.TxtMonths8.Disabled = true;
                        //this.TxtMonths9.Disabled = true;
                        //this.TxtMonths10.Disabled = true;
                        //this.TxtMonths11.Disabled = true;
                        //this.TxtMonths12.Disabled = true;
                        //this.TxtMonthsTotal.Disabled = true;
                    }
                }
            }
 
        }


        private void SetControl(string id)
        {
            Bbussness.entity = new V_CrmLayout();
            IList<V_CrmLayout> ICrmLayout = Bbussness.GetEntitys<V_CrmLayout>("DeptID='" + id + "'");
            if (ICrmLayout != null)
            {
                if (ICrmLayout.Count > 0)
                {
                    for (int i = 0; i < ICrmLayout.Count; i++)
                    {
                        this.ddlyear.Text = ICrmLayout[i].Years;
                        this.ddlyear.Enabled = false;
                        this.txtusername.Value = ICrmLayout[i].UName;
                        this.txtaddtime.Value = ICrmLayout[i].Addtime.ToString();
                        this.txtDeptID.Value = ICrmLayout[i].DeptName.ToString();
                        this.DeptID.Value = ICrmLayout[i].DeptID.ToString();
                        this.btnDeptID.Disabled = true;
                        if (ICrmLayout[i].Months.ToString().Trim() == "1")
                        {
                            this.TxtMonths1.Value = ICrmLayout[i].PlanSum.ToString();
                            this.txtExplain1.Value = ICrmLayout[i].Explain.ToString();
                        }
                        else if (ICrmLayout[i].Months.ToString().Trim() == "2")
                        {
                            this.TxtMonths2.Value = ICrmLayout[i].PlanSum.ToString();
                            this.txtExplain2.Value = ICrmLayout[i].Explain.ToString();
                        }
                        else if (ICrmLayout[i].Months.ToString().Trim() == "3")
                        {
                            this.TxtMonths3.Value = ICrmLayout[i].PlanSum.ToString();
                            this.txtExplain3.Value = ICrmLayout[i].Explain.ToString();
                        }
                        else if (ICrmLayout[i].Months.ToString().Trim() == "4")
                        {
                            this.TxtMonths4.Value = ICrmLayout[i].PlanSum.ToString();
                            this.txtExplain4.Value = ICrmLayout[i].Explain.ToString();
                        }
                        else if (ICrmLayout[i].Months.ToString().Trim() == "5")
                        {
                            this.TxtMonths5.Value = ICrmLayout[i].PlanSum.ToString();
                            this.txtExplain5.Value = ICrmLayout[i].Explain.ToString();
                        }
                        else if (ICrmLayout[i].Months.ToString().Trim() == "6")
                        {
                            this.TxtMonths6.Value = ICrmLayout[i].PlanSum.ToString();
                            this.txtExplain6.Value = ICrmLayout[i].Explain.ToString();
                        }
                        else if (ICrmLayout[i].Months.ToString().Trim() == "7")
                        {
                            this.TxtMonths7.Value = ICrmLayout[i].PlanSum.ToString();
                            this.txtExplain7.Value = ICrmLayout[i].Explain.ToString();
                        }
                        else if (ICrmLayout[i].Months.ToString().Trim() == "8")
                        {
                            this.TxtMonths8.Value = ICrmLayout[i].PlanSum.ToString();
                            this.txtExplain8.Value = ICrmLayout[i].Explain.ToString();
                        }
                        else if (ICrmLayout[i].Months.ToString().Trim() == "9")
                        {
                            this.TxtMonths9.Value = ICrmLayout[i].PlanSum.ToString();
                            this.txtExplain9.Value = ICrmLayout[i].Explain.ToString();
                        }
                        else if (ICrmLayout[i].Months.ToString().Trim() == "10")
                        {
                            this.TxtMonths10.Value = ICrmLayout[i].PlanSum.ToString();
                            this.txtExplain10.Value = ICrmLayout[i].Explain.ToString();
                        }
                        else if (ICrmLayout[i].Months.ToString().Trim() == "11")
                        {
                            this.TxtMonths11.Value = ICrmLayout[i].PlanSum.ToString();
                            this.txtExplain11.Value = ICrmLayout[i].Explain.ToString();
                        }
                        else if (ICrmLayout[i].Months.ToString().Trim() == "12")
                        {
                            this.TxtMonths12.Value = ICrmLayout[i].PlanSum.ToString();
                            this.txtExplain12.Value = ICrmLayout[i].Explain.ToString();
                        }


                    }

                }
            }
        }
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="Months">月份</param>
        /// <param name="type">类型</param>
        /// <param name="deptid">部门ID</param>
        /// <param name="explains">备注</param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public  string  Save(string Months,int  type,string deptid,string explains,string year)
        {
            string  returnstr="";
            string[] MonthList = Months.Split('︾');
            string[] ExplainList = explains.Split('︾');
               DatUser du = (DatUser)System.Web.HttpContext.Current.Session["User"]; //用户ID
            if (du == null)
            {
                returnstr = "用户信息有误";
            }
            if (ExplainList.Length == 0 || MonthList.Length == 0)
            {
                returnstr = "月份数量或者备注信息有误";
            }
            else
            {    
                    if (type == 1)
                    {
                        Bbussness.entity = new V_CrmLayout();
                        IList<V_CrmLayout> ICrmLayout = Bbussness.GetEntitys<V_CrmLayout>("Years='" + year + "' and DeptID='"+deptid+"'");
                        if (ICrmLayout != null&&ICrmLayout.Count>0)
                        {
                            returnstr = "相同年份的销售规划已经存在";
                        }
                        else
                        {
                            bool Iresult = false;
                            Bbussness.entity = new CrmLayout();
                            for (int i = 0; i < MonthList.Length; i++)
                            {
                                if (MonthList[i].Trim().Length == 0) continue;
                                CrmLayout layout = new CrmLayout();
                                layout.DeptID = new Guid(deptid);
                                layout.Months = (i+1).ToString();
                                layout.Years = year;
                                layout.PlanSum = decimal.Parse(MonthList[i]);
                                layout.Explain = ExplainList[i];
                                layout.AddUser = du.UserID;
                                layout.Addtime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd  HH:mm"));
                                Iresult = Bbussness.Insert(layout);

                            }
                            if (Iresult == false)
                            {
                                returnstr = "保存失败";
                            }
                            else
                            {
                                returnstr = "1";
                            }
                        }                   
                    }
                    else if (type == 2)
                    {
                        Bbussness.entity = new CrmLayout();
                        bool result = Bbussness.delete("DeptID='" + deptid + "' and Years='" + year + "'");
                        if (result == true)
                        {
                            bool Iresult = false;
                            Bbussness.entity = new CrmLayout();
                            for (int i = 0; i < MonthList.Length; i++)
                            {
                                if (MonthList[i].Trim().Length == 0) continue;
                                CrmLayout layout = new CrmLayout();
                                layout.DeptID = new Guid(deptid);
                                layout.Months = (i+1).ToString();
                                layout.Years = year;
                                layout.PlanSum = decimal.Parse(MonthList[i]);
                                layout.Explain = ExplainList[i];
                                layout.AddUser = du.UserID;
                                layout.Addtime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd  HH:mm"));
                                Iresult = Bbussness.Insert(layout);

                            }
                            if (Iresult == false)
                            {
                                returnstr = "保存失败";
                            }
                            else
                            {
                                returnstr = "1";
                            }
                        }

                    }
            }
            return returnstr;
        }

        
    }
}